package cn.edu.scau.cmi.wujiaxian.comprehensive.view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JFrame;

import cn.edu.scau.cmi.wujiaxian.comprehensive.dao.ClazzDao;
import cn.edu.scau.cmi.wujiaxian.comprehensive.dao.CourseDao;
import cn.edu.scau.cmi.wujiaxian.comprehensive.dao.SaveScoreRecord;
import cn.edu.scau.cmi.wujiaxian.comprehensive.dao.TeacherDao;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.Clazz;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.Course;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.ScoreInterval;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.Student;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.Teacher;
import cn.edu.scau.cmi.wujiaxian.comprehensive.service.StudentService;


public class Front {
	
	public static void main(String[] args) {
		EventQueue.invokeLater(() -> {
			@SuppressWarnings("unused")
			SIMS frame = new SIMS();
		});
	}
	
}

/**
 * 学生信息管理系统
 * @author lenovo
 *
 */
class SIMS extends JFrame {

	private static final long serialVersionUID = 210776600170374564L;
	
	//框的宽度
	private static final int DEFAULT_WIDTH = 800;
	//框的高度
	private static final int DEFAULT_HEIGHT = 700;
	
	//距离屏幕左边的距离
	private static final int DEFAULT_LEFT = 300;
	//距离屏幕顶部的距离
	private static final int DEFAULT_TOP = 100;
	
	//标题框的文字
	private static final String DEFAULT_TITLE = "Student Information Management System";
	
	//课程信息
	private Course course;
	private Clazz clazz;
	private Teacher teacher;
	private ArrayList<Student> students;
	
	public SIMS() {
		//初始化一些默认属性
		initial();
		//增加组件
		add(new ReportCardComp());
	} 
	
	/**
	 * 初始化框
	 */
	private void initial() {
//		临时属性
		ClazzDao clazzDao = new ClazzDao();
		clazz = (Clazz) clazzDao.select("id", 1).get(0);
		CourseDao courseDao = new CourseDao();
		course = (Course) courseDao.select("courseNumber", "SX001").get(0);
		TeacherDao teacherDao = new TeacherDao();
		teacher = (Teacher) teacherDao.select("id", course.getTeacherId()).get(0);
		StudentService service = new StudentService();
		students = service.selectStudents(clazz.getId(), course.getCourseNumber());
		
		//默认属性
		setLocation(DEFAULT_LEFT, DEFAULT_TOP);
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle(DEFAULT_TITLE);
		setResizable(false);
		setVisible(true);
	}
	
	/**
	 * 打印成绩单
	 * @author lenovo
	 *
	 */
	private class ReportCardComp extends JComponent {

		private static final long serialVersionUID = 7905765301612819887L;

		private int left;
		private int top;
		
		@Override
		protected void paintComponent(Graphics g) {
			//获取学生信息
			StudentService service = new StudentService();
			students = service.selectStudents(clazz.getId(), course.getCourseNumber());
			
			Graphics2D g2 = (Graphics2D) g;
			g2.setFont(new Font("Serif", Font.BOLD, 25));
			
			//设置背景颜色
			g2.setPaint(new Color(255, 255, 255));
			g2.fill(new Rectangle2D.Double(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT));
			
			//设置边框
			g2.setPaint(new Color(0, 0, 0));
			g2.setStroke(new BasicStroke(3));//设置线条宽度 
			left = 10;
			top = 10;
			g2.draw(new Rectangle2D.Double(left, top, DEFAULT_WIDTH - 35, DEFAULT_HEIGHT - 55));
			
			//设置标题
			top = top + 30;
			g2.drawString("期末考试成绩单", left + 260, top);
			top = top + 30;
			g2.drawString("课程：                     " + course.getName(), 20, top);
			top = top + 30;
			g2.drawString("教师：                     " + teacher.getName(), 20, top);
			top = top + 30;
			g2.drawString("班级：                     " + clazz.getGraceAndMajorAndClazz(), 20, top);
			
			//设置学生成绩清单
			g2.setStroke(new BasicStroke(2));
			top = top + 5;
			left = 20;
			g2.draw(new Line2D.Double(left, top, DEFAULT_WIDTH - 35, top));
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			g2.drawString("学号", left, top + 25);
			left = left + 215;
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			g2.drawString("姓名", left, top + 25);
			left = left + 130;
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			g2.drawString("考勤", left, top + 25);
			left = left + 80;
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			g2.drawString("测验", left, top + 25);
			left = left + 80;
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			g2.drawString("作业", left, top + 25);
			left = left + 80;
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			g2.drawString("期末", left, top + 25);
			left = left + 80;
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			g2.drawString("总评", left, top + 25);
			left = left + 80;
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			
			for (Student student : students) {
				top = top + 32;
				left = 20;
				g2.draw(new Line2D.Double(left, top, DEFAULT_WIDTH - 35, top));
				g2.draw(new Line2D.Double(left, top, left, top + 30));
				g2.drawString(student.getStudentNumber(), left, top + 25);
				left = left + 215;
				g2.draw(new Line2D.Double(left, top, left, top + 30));
				g2.drawString(student.getName(), left, top + 25);
				left = left + 130;
				g2.draw(new Line2D.Double(left, top, left, top + 30));
				g2.drawString(student.getScore().getAttendanceScore().toString(), left, top + 25);
				left = left + 80;
				g2.draw(new Line2D.Double(left, top, left, top + 30));
				g2.drawString(student.getScore().getTestScore().toString(), left, top + 25);
				left = left + 80;
				g2.draw(new Line2D.Double(left, top, left, top + 30));
				g2.drawString(student.getScore().getWorkScore().toString(), left, top + 25);
				left = left + 80;
				g2.draw(new Line2D.Double(left, top, left, top + 30));
				g2.drawString(student.getScore().getFinalScore().toString(), left, top + 25);
				left = left + 80;
				g2.draw(new Line2D.Double(left, top, left, top + 30));
				g2.drawString(student.getScore().getTotalScore().toString(), left, top + 25);
				left = left + 80;
				g2.draw(new Line2D.Double(left, top, left, top + 30));
			}
			
			top = top + 32;
			g2.draw(new Line2D.Double(20, top, DEFAULT_WIDTH - 35, top)); //底部闭合线
			
			//分数区间
			top = top + 32;
			left = 20;
			g2.draw(new Line2D.Double(left, top, DEFAULT_WIDTH - 355, top));
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			g2.drawString("成绩区段", left, top + 25);
			left = left + 215;
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			g2.drawString("人数", left, top + 25);
			left = left + 130;
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			g2.drawString("百分比", left, top + 25);
			left = left + 80;
			g2.draw(new Line2D.Double(left, top, left, top + 30));
			
			ArrayList<ScoreInterval> scoreIntervals = new ArrayList<>();
			scoreIntervals = service.getScoreIntervals(students);
			
			for (ScoreInterval scoreInterval : scoreIntervals) {
				top = top + 32;
				left = 20;
				g2.draw(new Line2D.Double(left, top, DEFAULT_WIDTH - 355, top));
				g2.draw(new Line2D.Double(left, top, left, top + 30));
				g2.drawString(scoreInterval.getScoreInterval(), left, top + 25);
				left = left + 215;
				g2.draw(new Line2D.Double(left, top, left, top + 30));
				g2.drawString(scoreInterval.getNumber().toString(), left, top + 25);
				left = left + 130;
				g2.draw(new Line2D.Double(left, top, left, top + 30));
				g2.drawString(String.format("%.2f",scoreInterval.getPercentage() * 100) + "%", left, top + 25);
				left = left + 80;
				g2.draw(new Line2D.Double(left, top, left, top + 30));
			}
			
			top = top + 32;
			g2.draw(new Line2D.Double(20, top, DEFAULT_WIDTH - 355, top)); //底部闭合线
			
			//把学生信息保存到文件
			SaveScoreRecord saveScoreRecord = new SaveScoreRecord();
			saveScoreRecord.save(students, course, teacher, clazz, scoreIntervals);
		}
		
	}
	
}


